home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / games2 / rotise12.zip / ROTISE.C < prev    next >
Text File  |  1993-03-28  |  6KB  |  228 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define R_VERSION "1.2"                        /* Version string */
  6. #ifdef __ANSI__
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #ifdef    __TURBOC__
  11. #include <io.h>
  12. #undef R_VERSION
  13. #define R_VERSION  "1.2 .. " __DATE__
  14. #endif
  15.  
  16. #ifdef __MSDOS__
  17. #include <conio.h>
  18. #include <stdlib.h>
  19. #endif
  20.  
  21. #define GLOBALS
  22. #include "rotise.h"
  23.  
  24. /* External data referenced */
  25. extern CMDLIST CmdTable[];                    /* commands.c - list of commands */
  26. extern BOOL Use_IBM_Chars;                    /* report.c - use funky ascii in report? */
  27.  
  28.     /* getopt() stuff */
  29. extern char *optarg;
  30. extern int opterr;
  31. extern int optind;
  32.  
  33. /* External routines used */
  34. int gettkline PROTO( ( FILE *, char *, int, int *, char **, int) );
  35.  
  36. /* Local data publicly available */
  37. char *TokenV[MAX_TOKENS];                    /* command line tokens */
  38. int TokenC;                                        /* How many tokens */
  39. int Cmd;                                            /* Command # being executed */
  40. int InputLine;                                    /* Line # of current file */
  41. BOOL Verbose = FALSE;                        /* How talkative to be */
  42. char *InputFilename;                            /* Filename being read */
  43.  
  44. /* Private data */
  45. char *Freelistfile;                            /* Filename for freelist */
  46. char *Worksheetfile;                            /* Filename for worksheet */
  47.     /* NONE */
  48.  
  49. int main ARGLIST( ( argc, argv ) )
  50.     NFARG( int argc )
  51.     FARG( char *argv[] )
  52. {
  53.     int ch;
  54.     int weeks;
  55.  
  56.     weeks = -1;                                /* Haven't got # of weeks yet */
  57.  
  58.     while ( ( ch = getopt( argc, argv, "iVf:v:s:w:" ) ) != EOF )
  59.     {    /* Parse command line arguments */
  60.         switch ( ch )
  61.         {
  62.             case 'i':                            /* Use IBM chars in report */
  63.                 Use_IBM_Chars = TRUE;
  64.                 break;
  65.             case 'f':                            /* Freelist */
  66.                Freelistfile = optarg;
  67.                 break;
  68.             case 's':                            /* Worksheet */
  69.                 Worksheetfile = optarg;
  70.                 break;
  71.             case 'v':                            /* Verbosity */
  72.                 if ( optarg == NULL )
  73.                     Verbose = 1;
  74.                 else
  75.                     Verbose = atoi( optarg );
  76.                 break;
  77.             case 'w':                            /* Weeks to init database to */
  78.                 weeks = atoi( optarg );
  79.                 break;
  80.             case 'V':
  81.                 printf("Version: %s.\n", R_VERSION );
  82.                 break;
  83.             default:
  84.                 fprintf( stderr, "Unknown switch: %c.\n", ch );
  85.             case '?':
  86.                 usage();
  87.                 exit( 1 );
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     /* Do datafiles */
  93.     if ( optind >= argc )
  94.     {    /* No datafiles specified? */
  95.         usage();
  96.         exit( 1 );
  97.     }
  98.  
  99.     if ( weeks <= 0 )
  100.         weeks = MAX_WEEKS;
  101.     pdb_init( 1, weeks );
  102.  
  103.     for ( ; optind < argc; ++optind )
  104.     {
  105.         VERBOSE( 1, "Parse file: %s.\n", argv[ optind ] );
  106.  
  107.         if ( !parse_file( argv[ optind ] ) )
  108.         { /* if we have problems with a file, just quit */
  109.             return 1;
  110.         }
  111.     }
  112.  
  113.     report();
  114.  
  115.     if ( Freelistfile != NULL )
  116.         freelist( Freelistfile );
  117.  
  118.     if ( Worksheetfile )
  119.         worksheet( Worksheetfile );
  120.  
  121.     return 0;
  122. }
  123.  
  124. void usage NOARGLIST
  125. {
  126.     fprintf( stderr, "Usage: rotise [-v[n] -V -i -wn -sfilename -ffilename] datafile\n" );
  127.     fprintf( stderr, "  Where:\n\tdatafile : data file to be parsed.\n");
  128.     fprintf( stderr, "\t-v : Verbose mode.\n");
  129.     fprintf( stderr, "\t-i : Use IBM chars in report.\n");
  130.     fprintf( stderr, "\t-sfilename : Generate worksheet.\n");
  131.     fprintf( stderr, "\t-wN : Init database to N weeks.\n");
  132.     fprintf( stderr, "\t-ffilename : Generate free agent list.\n");
  133.     fprintf( stderr, "\t-V : Print version.\n");
  134. }
  135.  
  136. /*
  137.     BOOL parse_file( char *fname )
  138.         Parse a rotisserie data file.
  139.  
  140.  ACCEPTS:
  141.     char *fname - name of file to be parsed
  142.  
  143.  RETURNS:
  144.     BOOL <value> - TRUE if okay, FALSE if couldn't open the file
  145. */
  146. BOOL parse_file ARGLIST( (fname) )
  147.     FARG( char *fname )
  148. {
  149.     char cmdline[100], *cmdstr;
  150.     int cmdX, tlen, cmdcnt;
  151.     FILE *fp;
  152.  
  153.     if ( (fp = fopen( fname, "r" )) == NULL )
  154.     { /* couldn't open command file, complain and return */
  155.         fprintf( stderr, "\n!ERROR! Unable to open file %s because\n", fname );
  156.         perror( "!ERROR! parse_data" );
  157.         return FALSE;
  158.     }
  159.  
  160.     InputLine = 0;
  161.     InputFilename = fname;
  162.  
  163.     while ( gettkline( fp, &cmdline[0], 100, &TokenC, &TokenV[0], MAX_TOKENS ) >= 0 )
  164.     { /* read in and parse each line */
  165.         InputLine++;
  166.  
  167.         if ( TokenC == 0 )
  168.         { /* we didn't get any arguments, try next line */
  169.             continue;
  170.         }
  171.  
  172.         VERBOSE( 6, "\tCommand: %s.\n", TokenV[0] );
  173.  
  174.         tlen = strlen( TokenV[0] );
  175.         cmdcnt = 0;
  176.         Cmd = -1;
  177.         for ( cmdX = 0; ( cmdstr = CmdTable[cmdX].cmd ) != NULL;
  178.                 ++cmdX )
  179.         { /* search thru table looking for command that matches */
  180.             if ( strnicmp( TokenV[0], cmdstr, tlen ) == 0 )
  181.             { /* matched for what was passed */
  182.                 Cmd = cmdX;
  183.                 cmdcnt++;
  184.             }
  185.             else if ( strnicmp( cmdstr, TokenV[0], strlen(cmdstr) ) == 0 )
  186.             { /* matches the other way */
  187.                 Cmd = cmdX;
  188.                 cmdcnt++;
  189.             }
  190.         }
  191.  
  192.         if ( cmdcnt == 1 )
  193.         { /* okay, found the command, so call routine */
  194.             if ( (*CmdTable[Cmd].cmd_func)( TokenC, &TokenV[0] ) == FALSE )
  195.             { /* Fatal error */
  196.                 return FALSE;
  197.             }
  198.         }
  199.         else if ( cmdcnt > 1 )
  200.         { /* ambiguous command */
  201.             fprintf( stderr, "File %s, Line %d: Ambiguous command - %s.\n", fname,
  202.                 InputLine, TokenV[0] );
  203.         }
  204.         else
  205.         { /* no command by that name */
  206.             fprintf( stderr, "File %s, Line %d: Unknown command - %s.\n", fname,
  207.                 InputLine, TokenV[0] );
  208.         }
  209.     }
  210.  
  211.     return TRUE;
  212. }
  213.  
  214.  
  215. /**********************/
  216. /* rdb_error - print out an error message for the interpreter
  217. /*********************/
  218.  
  219. void rdb_error ARGLIST( (s1, s2) )
  220.     NFARG(     char *s1 )
  221.     FARG(         char *s2 )
  222. {
  223.     fprintf( stderr, "!ERROR!File %s, Line %d: ", InputFilename, InputLine );
  224.     fprintf( stderr, "%s %s\n", s1, s2 );
  225. }
  226.  
  227.  
  228.